// 导入数据库操作
const db = require('../db/index')

// 获取角色处理函数
exports.roles = (req, res) => {
    const size = req.query.pageSize || 5
    const page = req.query.currentPage || 1
    const offset = (page - 1) * size
    const sql = `SELECT id,role_name,description FROM roles limit ${offset},${size}`
    db.query(sql, (err, results) => {
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (!results) return res.send({
            status: 1,
            message: '获取角色信息失败!'
        })
        const sql1 = 'SELECT COUNT(role_name)  AS total FROM `roles` '
        db.query(sql1, (err, results1) => {
            if (err) return res.send({
                status: 1,
                message: err
            })
            if (!results1) return res.send({
                status: 1,
                message: '获取角色名称失败!'
            })
            const total = results1.map(item => {
                return item.total
            })
            return res.send({
                status: 0,
                message: '获取角色信息成功!',
                data: results,
                total: total.join()
            })
        })

    })
}
// 获取所有角色处理函数
exports.allRoles = (req, res) => {
    const sql = 'SELECT id,role_name,description FROM roles'
    db.query(sql, (err, results) => {
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (!results) return res.send({
            status: 1,
            message: '获取角色名称失败!'
        })
        return res.send({
            status: 0,
            message: '获取角色名称成功!',
           data: results
        })
    })
}

// 删除角色处理函数
exports.delRoles = (req, res) => {
    const role_id = Number(req.query.role_id)
    const sql = 'DELETE FROM roles WHERE id=?'
    db.query(sql, role_id, (err, results) => {
        if (err) return res.send({
            status: 1,
            message: err
        })
        // if(results.affectedRows!==0) return res.send({
        //     status:1,
        //     message:'删除角色失败!'
        // })
        return res.send({
            status: 0,
            message: '删除角色成功!',
            data: null
        })
    })

}

// 添加角色处理函数
exports.addRoles = (req, res) => {
    const sql1 = 'SELECT * FROM roles WHERE role_name=?'
    db.query(sql1, req.body.role_name, (err, results) => {
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (results.length > 0) return res.send({
            status: 0,
            message: '该角色已存在!'
        })
        const sql = 'INSERT INTO roles SET ?'
        db.query(sql, {role_name: req.body.role_name, description: req.body.description}, (err, results) => {
            if (err) return res.send({
                status: 1,
                message: err
            })
            if (results.affectedRows !== 1) return res.send({
                status: 1,
                message: '添加角色失败!'
            })
            return res.send({
                status: 0,
                message: '添加角色成功!',
                data: null
            })
        })
    })
}

// 更新角色处理函数
exports.updateRoles = (req, res) => {
    const sql = 'UPDATE roles set role_name =?, description=? WHERE id = ?'
    db.query(sql, [req.body.role_name, req.body.description, req.query.role_id], (err, results) => {
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (results.affectedRows !== 1) return res.send({
            status: 1,
            message: '更新角色失败!'
        })
        return res.send({
            status: 0,
            message: '更新角色成功!',
            data: null
        })
    })
}

// 查询角色处理函数
exports.searchRoles = (req, res) => {
    const sql = `SELECT id,role_name,description FROM roles WHERE role_name LIKE '%${req.query.role_name}%'`
    db.query(sql, (err, results) => {
        if (err) return res.send({
            status: 1,
            message: err
        })
        if (results.length <= 0) return res.send({
            status: 1,
            message: '查询角色失败!'
        })
        return res.send({
            status: 0,
            message: '查询角色成功!',
            data: results
        })
    })
}